cmake_minimum_required(VERSION 3.3)
find_package(Protobuf 3.2 REQUIRED)
if ( NOT (PROTOBUF_FOUND))
    message(FATAL_ERROR "Please Compile and Install ProtoBuffer -version 3 using GRPC C++: CMake will Exit")
endif()

MESSAGE(${PROTOBUF_PROTOC_EXECUTABLE})

#get_filename_component(PROTO_PATH ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
#message("****${PROTO_PATH}***:" ${PROTO_PATH})
#message(${CMAKE_SOURCE_DIR}  #project dir
#message(${CMAKE_CURRENT_SOURCE_DIR}) # current project dir

set(PROTO_PATH ${CMAKE_CURRENT_SOURCE_DIR}/protos)
message("---PROTO_PATH:---" ${PROTO_PATH})
set(GENERATED_PROTOBUF_PATH ${CMAKE_CURRENT_SOURCE_DIR}/generated)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

file(GLOB_RECURSE proto_files *.proto)

set(OUT_HDR_NAME gc_msg_interface.h)
set(OUT_HDR ${GENERATED_PROTOBUF_PATH}/${OUT_HDR_NAME})
file(WRITE ${OUT_HDR} "\/\/ This file is generated automatically by CMake, dont modify it manully. ^_^\n")
file(APPEND ${OUT_HDR} "#ifndef GC_MSG_INTERFACE_H\n")
file(APPEND ${OUT_HDR} "#define GC_MSG_INTERFACE_H\n\n")


execute_process(COMMAND  which grpc_cpp_plugin OUTPUT_VARIABLE GRPC_CPP_PLUGIN)
string(STRIP ${GRPC_CPP_PLUGIN} GRPC_CPP_PLUGIN)



message("******proto_files***:" ${proto_files})
message("******GENERATED_PROTOBUF_PATH******:" ${GENERATED_PROTOBUF_PATH})
foreach(proto_file ${proto_files})
    STRING(REGEX REPLACE ".+/(.+)\\..*" "\\1" FILE_NAME ${proto_file})
    message("proto_file:" ${proto_file})
    message("FILE_NAME:" ${FILE_NAME})
    add_custom_command(
            OUTPUT  "${FILE_NAME}.pb.cc"
            "${FILE_NAME}.pb.h"
            COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
            ARGS "--proto_path=${PROTO_PATH}"
            "--cpp_out=${GENERATED_PROTOBUF_PATH}"
            "${proto_file}"
    )

    list(APPEND GENERATED_FILES
            "${GENERATED_PROTOBUF_PATH}/${FILE_NAME}.pb.cc"
            )

    list(APPEND OUT_FILES ${FILE_NAME}.pb.cc ${FILE_NAME}.pb.h)

    file(APPEND ${OUT_HDR} "#include \"${FILE_NAME}.pb.h\"\n")

    add_custom_command(
            OUTPUT   "${FILE_NAME}.grpc.pb.h"
            "${FILE_NAME}.grpc.pb.cc"
            COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
            ARGS "--proto_path=${PROTO_PATH}"
            "--grpc_out=${GENERATED_PROTOBUF_PATH}"
            "--plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN}"
            "${proto_file}"
    )

    list(APPEND GENERATED_GRPC_FILES
            "${GENERATED_PROTOBUF_PATH}/${FILE_NAME}.grpc.pb.cc"
            )

    list(APPEND OUT_GRPC_FILES ${FILE_NAME}.grpc.pb.cc ${FILE_NAME}.grpc.pb.h)

    file(APPEND ${OUT_HDR} "#include \"${FILE_NAME}.grpc.pb.h\"\n")
endforeach()

file(APPEND ${OUT_HDR} "\n#endif\n")

# build the lib

message("OUT_FILES:" ${OUT_FILES})
message("OUT_GRPC_FILES:" ${OUT_GRPC_FILES})
add_library(qpbs ${GENERATED_FILES} ${GENERATED_GRPC_FILES} ${GENERATED_PROTOBUF_PATH}/gc_msg_interface.h)
target_include_directories(qpbs PUBLIC
        ${CMAKE_CURRENT_BINARY_DIR} ${PROTOBUF_INCLUDE_DIR})
target_link_libraries(qpbs PUBLIC libprotobuf.a)